package com.huiqiao.stat.controller;

import com.huiqiao.bus.domain.Customer;
import com.huiqiao.bus.domain.Rent;
import com.huiqiao.bus.service.ICustomerService;
import com.huiqiao.bus.service.IRentService;
import com.huiqiao.sys.utils.ExportHSSFCellStyle;
import com.huiqiao.sys.utils.ExportRentUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-18  17:08
 */

@RestController
@RequestMapping("/stat")
public class ExcelExportController {

    @Autowired
    private IRentService rentService;

    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/exportRent")
    public ResponseEntity<Object> exportRent(String rentid) {
        //根据出租单号查询出出租信息
        Rent rent = rentService.queryRentByRentId(rentid);
        //根据身份证号查询客户信息
        Customer customer = customerService.queryCustomerByIdentity(rent.getIdentity());
        String fileName=customer.getCustname()+"-的出租单.xls";
        String sheetName=customer.getCustname()+"出租单";
        ByteArrayOutputStream bos = ExportRentUtils.exportRent(rent, customer, sheetName);
        try {
            //处理文件名乱码
            fileName =  URLEncoder.encode(fileName, "UTF-8");
            //创建，封装响应头信息的对象
            HttpHeaders headers = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件名称
            headers.setContentDispositionFormData("attachment", fileName);
            //创建ResponseEntity对象
            return new ResponseEntity<Object>(bos.toByteArray(), headers, HttpStatus.CREATED);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }

        return null;

    }


    @RequestMapping("/exportAllRents")
    public ResponseEntity<Object> exportAllRents() {
        // 获取所有出租单信息
        List<Rent> rents = rentService.queryAllRents();
        String fileName = "所有出租单.xls";
        String sheetName = "所有出租单";
        ByteArrayOutputStream bos = this.exportAllRents(rents, sheetName);
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);
            return new ResponseEntity<Object>(bos.toByteArray(), headers, HttpStatus.CREATED);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }


    public  ByteArrayOutputStream exportAllRents(List<Rent> rents, String sheetName) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle baseStyle = ExportHSSFCellStyle.createBaseStyle(workbook);
        HSSFCellStyle titleStyle = ExportHSSFCellStyle.createTitleStyle(workbook);
        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setDefaultColumnWidth(30);

        // 创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("出租单号");
        titleRow.createCell(1).setCellValue("身份证号");
        titleRow.createCell(2).setCellValue("起租时间");
        titleRow.createCell(3).setCellValue("还车时间");
        titleRow.createCell(4).setCellValue("出租状态");
        titleRow.createCell(5).setCellValue("车牌号");
        titleRow.createCell(6).setCellValue("出租价格");
        titleRow.createCell(7).setCellValue("操作员");
        titleRow.createCell(8).setCellValue("操作时间");

        // 填充数据
        int rowIndex = 1;
        for (Rent rent : rents) {
            HSSFRow row = sheet.createRow(rowIndex++);
            row.createCell(0).setCellValue(rent.getRentid());
            // 假设你有一个方法来获取与每个Rent关联的客户信息
//             Customer customer = customerService.queryCustomerByIdentity(rent.getIdentity());
            row.createCell(1).setCellValue(rent.getIdentity());
            row.createCell(2).setCellValue(rent.getBegindate().toLocaleString());
            row.createCell(3).setCellValue(rent.getReturndate().toLocaleString());
            if(rent.getRentflag() == 1){
                row.createCell(4).setCellValue("已出租");
            }
            else {
                row.createCell(4).setCellValue("未出租");
            }
            row.createCell(5).setCellValue(rent.getCarnumber());
            row.createCell(6).setCellValue(rent.getPrice());
            row.createCell(7).setCellValue(rent.getOpername());
            row.createCell(8).setCellValue(rent.getCreatetime().toLocaleString());
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bos;
    }



}
